package com.lkd.http.process;

import com.lkd.exception.LogicException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 全局异常拦截
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = LogicException.class)
    public ResponseEntity<String> exceptionHandler(LogicException e){
        log.error("controller call error",e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = DuplicateKeyException.class)
    public ResponseEntity<String> exceptionHandler(DuplicateKeyException e){
        log.error("controller call error",e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("已存在此名称的对象");
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception e){
        log.error("error",e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<String> exceptionHandler(MethodArgumentNotValidException e){
        boolean b = e.getBindingResult().hasErrors();
        String msg = "";
        if(b){

            List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
//            for (ObjectError error : allErrors) {
//                String defaultMessage = error.getDefaultMessage();
//                if(msg.length()>0){
//                    msg += ",";
//                }
//                msg += defaultMessage;
//            }
            msg = allErrors.
                    stream().
                    map(ObjectError::getDefaultMessage)
                    .collect(Collectors.joining(","));
        }
        log.error("error",msg);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg);
    }
}
